package org.andersonlab.trailsdm.data;

/*
 * 
Copyright (c) 2010 The Regents of the University of California.
All rights reserved.
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that the above
copyright notice and the following two paragraphs appear in all copies
of this software.

IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
ENHANCEMENTS, OR MODIFICATIONS..
 */



import com.thoughtworks.xstream.XStream;
import java.util.LinkedHashSet;
import org.andersonlab.trailsdm.content.Content;
import org.andersonlab.trailsdm.control.DataConduit;
import org.andersonlab.trailsdm.control.Datum;
import org.andersonlab.trailsdm.control.DisplayController;
import org.andersonlab.trailsdm.data.Trail;

/**
 * The DatumContainer isn't something that exists in the final version of the code.
 * 
 * It is just a proxy for GWT server-side functionality.  It's needed to be able
 * to do any testing of this code.  It provides serialization functionality using
 * XStream, hence the one big list of library dependencies.
 * 
 * What really happens in the GWT code is much more elaborate
 * 
 * @author John Christopher Anderson
 */


public class DatumContainer implements DataConduit, DisplayController {
    //These control where the Trail is right now
    private Trail currentTrail;
    private int currentIndex = 0;
    
    //These are just for serialization
    private XStream xstream = new XStream();
    private static final String DATAPATH = "C:/Users/jcanderson/Documents/NetBeansProjects/TrailsDM/data/";
    
    @SuppressWarnings("LeakingThisInConstructor")
    public DatumContainer() {
        Datum.setConduit(this);
        Datum.setDisplayer(this);
    }
    
    @Override
    public void retrieve(String uuid, RetrievalCallBack callback) {
        try {
            String filepath = DATAPATH + uuid;
            String xml = FileUtils.readFile(filepath);
            Datum datum = (Datum) xstream.fromXML(xml);
            callback.onSuccess(datum);
        } catch(Exception err) {
            callback.onFailure();
        }
    }

    @Override
    public void retrieveList(LinkedHashSet<String> uuids, ListCallBack callback) {
        LinkedHashSet<Datum> listy = new LinkedHashSet<Datum>();
        for(String uuid : uuids) {
            try {
                String filepath = DATAPATH + uuid;
                String xml = FileUtils.readFile(filepath);
                Datum datum = (Datum) xstream.fromXML(xml);
                listy.add(datum);
            } catch(Exception err) {
                callback.onFailure();
            }
        }
        callback.onSuccess(listy);

    }
    
    @Override
    public void save(Datum datum, ChangeCallBack callback) {
        try {
            String filepath = DATAPATH + datum.getUUID();
            String xml = xstream.toXML(datum);
            FileUtils.writeFile(xml, filepath);
        } catch(Exception err) {
            err.printStackTrace();
            callback.wasSuccessful(false);
        }
        callback.wasSuccessful(true);
    }

    @Override
    public void loadTrail(Trail trail) {
        currentTrail = trail;
    }

    @Override
    public Trail getCurrentTrail() {
        return currentTrail;
    }

    @Override
    public void setTrailEditMode() {
        System.out.println("Browser entering EDIT mode on the Trail");
    }

    @Override
    public void setTrailRunMode() {
        System.out.println("Browser entering RUN mode on the Trail");
    }

    @Override
    public void goForwardOrReverse(int howmany) {
        try {
            currentIndex +=howmany;
            System.out.print("Advancing content browsing by " + howmany);
            
            currentTrail.getContentAt(currentIndex, new RetrievalCallBack<Content>() {

                @Override
                public void onSuccess(Content datum) {
                    System.out.println(" to page " + datum.getName());
                }

                @Override
                public void onFailure() {
                    System.out.println("...but it failed");
                }
            });
            System.out.println("...but it failed");
        } catch(Exception err) {
            System.out.println("          ...but it failed");
            err.printStackTrace();
        }
    }

    @Override
    public void goTo(int index) {
    }

    @Override
    public void divertTo(Content content) {
    }

}
